import com.jlu.mcloud.communicate.fileio.FileEntity;
import com.jlu.mcloud.communicate.fileio.FileIoHandler;
import com.jlu.mcloud.communicate.fileio.FileReader;
import com.jlu.mcloud.communicate.fileio.FileWriter;
import com.jlu.mcloud.communicate.heartbeat.Command;
import com.jlu.mcloud.communicate.heartbeat.HeartbeatClient;
import com.jlu.mcloud.rpc.client.RPCClient;
import com.jlu.mcloud.service.HelloService;
import com.jlu.p2pserver.IFileQuery;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author freedom
 * @DATE 2017/5/16 0016 21:46.
 * see
 */
public class AndroidClient {

    private static HeartbeatClient client = new HeartbeatClient() {
        @Override
        protected boolean processCommand(Command command) {
            System.out.println(command.getInfo().get("ack"));
            return true;
        }

        @Override // setinfo
        protected Map<String, Object> getInfo() {
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("name", "freedom");
            return info;
        }

        @Override
        protected String getNodeId() {
            return "123456";
        }
    };

    private static InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8089);

    public static void main(String[] args) {
//       IFileQuery fileQuery = RPCClient.getRemoteProxyObject(IFileQuery.class,new InetSocketAddress(
//               "127.0.0.1",8089
//       ));
//        System.out.println(fileQuery.queryFiles());
        FileIoHandler fileIoHandler = RPCClient.getRemoteProxyObject(FileIoHandler.class,
                new InetSocketAddress("127.0.0.1", 8089));
        try {
            FileEntity fileEntity = fileIoHandler.readFile("changba_log730.txt");
            FileOutputStream outputStream = new FileOutputStream("test.txt");
            outputStream.write(fileEntity.getData());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static byte[] toArrayByte(File file) throws IOException {
        FileChannel fileChannel = null;
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(file);
            fileChannel = fileInputStream.getChannel();
            ByteBuffer e = ByteBuffer.allocate((int)fileChannel.size());

            while(fileChannel.read(e) > 0) {
                ;
            }

            byte[] var4 = e.array();
            return var4;
        } catch (IOException var8) {
            throw var8;
        } finally {
            if(fileChannel != null) {
                fileChannel.close();
            }

            if(fileInputStream != null) {
                fileInputStream.close();
            }

        }
    }
}
